package com.kandinfo.base.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kandinfo.base.admin.constant.WorkGroupDataConstant;
import com.kandinfo.base.admin.domain.WorkGroupData;
import com.kandinfo.base.admin.dto.WorkGroupDataDto;
import com.kandinfo.base.admin.repository.WorkGroupDataRepository;
import com.kandinfo.base.admin.service.WorkGroupDataService;
import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.base.security.SecurityContext;


@Service
public class WorkGroupDataServiceImpl extends AbstractService<WorkGroupData, Long, WorkGroupDataRepository> implements
        WorkGroupDataService {
    @Autowired
    private WorkGroupDataRepository workGroupDataRepository;

    @Resource
    public void setWorkGroupDataRepository(WorkGroupDataRepository workGroupDataRepository) {
        super.setRepository(workGroupDataRepository);
    }

    @Override
    public List<WorkGroupDataDto> findByGroupId(Long groupId, Integer type) {
        List<WorkGroupDataDto> dtolist = new ArrayList<WorkGroupDataDto>();
        List<WorkGroupData> wgdList = workGroupDataRepository.findByGroupId(groupId, type);
        for (WorkGroupData workGroupData : wgdList) {
            WorkGroupDataDto dto = new WorkGroupDataDto();
            dto.setId(workGroupData.getId());
            dto.setGroupId(workGroupData.getGroupId());
            Long refId = workGroupData.getRefId();
            dto.setRefId(refId);
            dtolist.add(dto);
        }
        return dtolist;
    }

    @Override
    public List<WorkGroupData> getByQuestionBankId(Long questionBankId) {
        return workGroupDataRepository.getByQuestionBankId(questionBankId);
    }

    /**
     * 
     * Description: 根据关联id查询用户组关联信息
     *
     * @param refId
     * @param type
     *            数据权限类型
     * @return
     */
    @Override
    public List<WorkGroupData> findByRefId(Long refId, Integer type) {
        return workGroupDataRepository.findByRefId(refId, type);
    }

    /**
     * 
     * Description: 根据分组id批量保存
     *
     * @param refIds
     * @param groupId
     * @param type
     * @return
     */
    @Transactional
    public boolean saveWGDatesByGroupId(String refIds, Long groupId, Integer type) {
        if (StringUtils.isBlank(refIds)) {
            return true;
        }

        // 删除所有关联数据
        List<WorkGroupData> list = workGroupDataRepository.findByGroupId(groupId, type);
        deleteInBatch(list);

        String[] idArr = refIds.split(",");

        WorkGroupData wgDate = null;
        Long userid = SecurityContext.getInstance().getCurrentUserId();
        List<WorkGroupData> wgDates = new ArrayList<WorkGroupData>();
        for (String id : idArr) {
            wgDate = new WorkGroupData();
            wgDate.setRefId(Long.valueOf(id));
            wgDate.setGroupId(groupId);
            wgDate.setRefType(type);
            wgDate.setCreateBy(userid);
            wgDate.setCreateTs(new Date());
            wgDates.add(wgDate);
        }
        save(wgDates);

        return true;

    }

    /**
     * 
     * Description: 根据用户组集合保存题源权限数据
     *
     * @param groupIds
     *            workgroupid 集合
     * @param qsId
     *            题源id
     * @return
     */
    @Override
    @Transactional
    public boolean saveWGDates(String groupIds, Long qsId) {
        if (StringUtils.isBlank(groupIds)) {
            return true;
        }

        String[] idArr = groupIds.split(",");

        WorkGroupData wgDate = null;
        Long userid = SecurityContext.getInstance().getCurrentUserId();
        List<WorkGroupData> wgDates = new ArrayList<WorkGroupData>();
        for (String id : idArr) {
            wgDate = new WorkGroupData();
            wgDate.setRefId(qsId);
            wgDate.setGroupId(Long.valueOf(id));
            wgDate.setRefType(WorkGroupDataConstant.TYPE_QUESTIONSOURCE);
            wgDate.setCreateBy(userid);
            wgDate.setCreateTs(new Date());
            wgDates.add(wgDate);
        }
        save(wgDates);

        return true;
    }

    /**
     * 
     * Description: 更新题源id下的所有题源权限数据
     *
     * @param groupIds
     *            workgroupid 集合
     * @param qsId
     *            题源id
     * @return
     */
    @Transactional
    public boolean editWGDates(String groupIds, Long qsId) {
        // 删除所有关联数据
        List<WorkGroupData> list = findByRefId(qsId, WorkGroupDataConstant.TYPE_QUESTIONSOURCE);
        deleteInBatch(list);

        // 重新保存信息
        saveWGDates(groupIds, qsId);

        return true;
    }

    @Override
    public List<WorkGroupData> findPermissionByUserId(Long userId, Integer type) {
        return workGroupDataRepository.findPermissionByUserId(userId, type);
    }

}
